package com.yzw.controller;

import com.yzw.dto.DataGridResult;
import com.yzw.dto.QueryDTO;
import com.yzw.pojo.SysRole;
import com.yzw.pojo.SysUser;
import com.yzw.service.SysRoleService;
import com.yzw.service.SysUserService;
import com.yzw.utils.R;
import com.yzw.utils.ShiroUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
public class SysRoleController {

    @Autowired
    private SysRoleService sysRoleService;

    /**
     * 打野查询+模糊搜索
     * @param queryDTO
     * @return
     */
    @RequestMapping("/sys/role/list")
    public DataGridResult findPage( QueryDTO queryDTO){
        DataGridResult page = sysRoleService.findPage(queryDTO);
        return page;
    }

    @RequestMapping("/sys/role/roleAll")
    public R roleAll(){
       List<SysRole> sysRoles = sysRoleService.roleAll();
        return R.ok().put("sites",sysRoles);
    }

    /**
     * 新增
     * @param sysRole
     * @return
     */
    @RequestMapping("/sys/role/save")
    public R save(@RequestBody SysRole sysRole){
        int i = sysRoleService.save(sysRole);
        if (i<1){
            return R.error("新增失败");
        }
        return R.ok("新增成功");
    }

     @RequestMapping("/sys/role/del")
    public R deleteUser(@RequestBody List<Long> ids){
         for (Long id : ids) {
             if (id<=6){
                 return R.error("系统设置，不允许删除");
             }
         }
         try {
             int i =  sysRoleService.deleteRole(ids);
             return R.ok("删除成功");
         } catch (Exception e) {
             e.printStackTrace();
             return R.error("删除失败");
         }
    }


    @RequestMapping("/sys/role/info/{roleId}")
    public R findRoleById(@PathVariable("roleId") long roleId){

        SysRole sysRole = null;
        try {
            sysRole = sysRoleService.findRoleById(roleId);
            return R.ok().put("role",sysRole);
        } catch (Exception e) {
            e.printStackTrace();
            return R.error("操作失败");
        }
    }

     @RequestMapping("/sys/role/update")
    public R update(@RequestBody SysRole sysRole){

         try {
             sysRoleService.update(sysRole);
             return R.ok().put("role",sysRole);
         } catch (Exception e) {
             e.printStackTrace();
             return R.error("修改失败");
         }

    }

}
